package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.common.R;
import com.itheima.dto.DishDto;
import com.itheima.mapper.DishFlavorMapper;
import com.itheima.mapper.DishMapper;
import com.itheima.pojo.Dish;
import com.itheima.pojo.DishFlavor;

import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private DishMapper dishMapper;
    @Resource
    private DishFlavorMapper dishFlavorMapper;



//    @Override
//    @Transactional
//    public int saveDish(DishDto dishDto) {
////        dishDto.setCreateTime(LocalDateTime.now());
////        dishDto.setUpdateTime(LocalDateTime.now());
////
////        Employee employee = (Employee) session.getAttribute("employee");
////        dishDto.setCreateUser(employee.getId());
////        dishDto.setUpdateUser(employee.getId());
//        //保存菜品数据
//        dishMapper.insert(dishDto);
//        List<DishFlavor> flavors = dishDto.getFlavors();
//
//        //判断是否选择了口味
//        if (flavors != null && flav ors.size() > 0) {
//            //保存菜品的口味数据 需要注意：DishFlavor中dishId是没有数据的，
//            // 所以需要手动设置
//            Long dishId = dishDto.getId();//获取菜品id
//            for (DishFlavor flavor : flavors) {
//                flavor.setDishId(dishId);
//                dishFlavorMapper.insert(flavor);
//            }
//        }
//
//        return 0;
//    }

    @Override
    public int updateDish(DishDto dishDto) {
//        dishDto.setCreateTime(LocalDateTime.now());
//        dishDto.setUpdateTime(LocalDateTime.now());
//
//        Employee employee = (Employee) session.getAttribute("employee");
//        dishDto.setCreateUser(employee.getId());
//        dishDto.setUpdateUser(employee.getId());


        List<DishFlavor> flavors = dishDto.getFlavors();
        LambdaQueryWrapper<Dish> qw = new LambdaQueryWrapper<>();
        qw.eq(Dish ::getId ,dishDto.getId() );
        int update = dishMapper.update(dishDto, qw);


        if (flavors.isEmpty() && flavors.size() > 0) {
            Long dtoId = dishDto.getId();
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dtoId);
                dishFlavorMapper.insert(flavor);
            }
        }

        return update;
    }


    @Override
    public DishDto getselectupdate(Long id) {

//        //查询菜品基本信息，从dish表查询
//        Dish dish = dishMapper.selectById(id);
//        DishDto dishDto = new DishDto();
//        BeanUtils.copyProperties(dish,dishDto);
////查询当前菜品对应的口味信息，从dish_flavor表查询
//        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(DishFlavor::getDishId,dish.getId());
//        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
//        dishDto.setFlavors(flavors);

        Dish one = dishMapper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(one,dishDto);
        LambdaQueryWrapper<DishFlavor> lqw= new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,one.getId());

        List<DishFlavor> list = dishFlavorMapper.selectList(lqw);

        dishDto.setFlavors(list);


        return dishDto;
    }

    @Override
    public int updateAdd(DishDto dishDto) {

        int i = dishMapper.updateById(dishDto);

        LambdaQueryWrapper<DishFlavor> qw = new LambdaQueryWrapper<>();
        qw.eq(DishFlavor ::getDishId,dishDto.getId() );

        dishFlavorService.remove(qw);

        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item)->{
            item.setDishId(dishDto.getId());
            return item;

        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

        return i;
    }









}